package com.example.wechattest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.MenuPopupHelper;
import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.viewpager2.widget.ViewPager2;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wechattest.myAdapter.MyAdapter;
import com.google.android.material.navigation.NavigationView;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private Toolbar tb;
    private ViewPager2 vp;
    private TextView tb_text;
    private LinearLayout bottomWechat;
    private LinearLayout bottomConnect;
    private LinearLayout bottomFind;
    private LinearLayout bottonMiddle;
    private List<LinearLayout> linearLayouts = new ArrayList<>();


    private String titles[] = {"微信", "通讯录", "发现"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        initDate();


        //设置头部导航栏
//        tb.inflateMenu(R.menu.toolbar_menu);
        setSupportActionBar(tb);
        tb_text.setText("微信");

        //碎片
        MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(), getLifecycle(), this, 3);
        vp.setAdapter(myAdapter);

        //抽屉设置
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, tb, R.string.app_name, R.string.app_name);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();
        //TODO:菜单抽屉点击

        //设置vp
        vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                tb_text.setText(titles[position]);
                bottonMiddle.setSelected(false);
                linearLayouts.get(position).setSelected(true);
                bottonMiddle = linearLayouts.get(position);
            }
        });
    }

    private void init() {
        tb = (Toolbar) findViewById(R.id.tb);
        vp = (ViewPager2) findViewById(R.id.vp);
        tb_text = findViewById(R.id.tb_text);

        bottomWechat = (LinearLayout) findViewById(R.id.bottom_wechat);
        bottomConnect = (LinearLayout) findViewById(R.id.bottom_connect);
        bottomFind = (LinearLayout) findViewById(R.id.bottom_find);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        navigationView = (NavigationView) findViewById(R.id.navigationView);

    }

    /**
     * 加载数据
     */
    private void initDate() {
        tb.setTitle("");

        linearLayouts.add(bottomWechat);
        linearLayouts.add(bottomConnect);
        linearLayouts.add(bottomFind);
        bottomWechat.setOnClickListener(this);
        bottomConnect.setOnClickListener(this);
        bottomFind.setOnClickListener(this);
        bottomWechat.setSelected(true);
        bottonMiddle = bottomWechat;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_menu,menu);
        return true;
    }

    @SuppressLint("RestrictedApi")
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {


        switch (item.getItemId()) {
            case R.id.menu_find:
                //TODO:页面跳转find
                break;
            case R.id.menu_add:
                //TODO：弹出菜单
                View menuAdd = tb.findViewById(R.id.menu_add);
                PopupMenu popupMenu = new PopupMenu(MainActivity.this,menuAdd);
                popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu());

                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(MainActivity.this, "111", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });
                try {
                    Field field = popupMenu.getClass().getDeclaredField("mPopup");
                    field.setAccessible(true);
                    MenuPopupHelper mHelper = (MenuPopupHelper) field.get(popupMenu);
                    mHelper.setForceShowIcon(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    popupMenu.show();
                }
                break;
        }

        return true;
    }

    private void setIconEnable(Menu menu, boolean enable)
    {
        try
        {
            Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
            Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
            m.setAccessible(true);
            //传入参数
            m.invoke(menu, enable);

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        //tablayout监听
        for (int i = 0; i < linearLayouts.size(); i++) {
            if (linearLayouts.get(i).getId() == view.getId()){
                vp.setCurrentItem(i);
            }
        }
    }
}